﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _06_委托练习
{
    public partial class MainForm : Form
    {
        //移动的状态
        private PointState pointState;
        //步长
        private int step = 1;
        //定义静态泛型委托=>用于设置运动状态
        public static Action<PointState> SetPointState;
        //定义静态泛型委托=>用于设置移动位置
        public static Action<int, int> SetButtonLocation;
        //P2P运动窗体
        Frm_P2PMove frm_P2PMove=null;
        //定义静态范围委托=>用于设置步长
        public static Action<int> SetStep;
        //定义静态泛型委托=>用于设置间隔
        public static Action<int> SetIntenal;


        public MainForm()
        {
            InitializeComponent();
            //实例化委托对象
            SetPointState = state => pointState = state;
            //实例化委托对象=>设置按钮位置
            SetButtonLocation = (x, y) => this.button1.Location = new Point(x, y);
            //实例化委托对象=>设置步长
            SetStep = value =>step = value;
            //实例化委托对象=>设置间隔
            SetIntenal = value => this.timer1.Interval = value;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            switch (pointState)
            {
                case PointState.None:
                    break;
                case PointState.Up:
                    this.button1.Location = new Point(this.button1.Location.X, this.button1.Location.Y - step);
                    break;
                case PointState.Down:
                    this.button1.Location = new Point(this.button1.Location.X, this.button1.Location.Y + step);
                    break;
                case PointState.Left:
                    this.button1.Location = new Point(this.button1.Location.X - step, this.button1.Location.Y);
                    break;
                case PointState.Right:
                    this.button1.Location = new Point(this.button1.Location.X + step, this.button1.Location.Y);
                    break;
            }
            //显示X坐标
            this.toolStripStatusLabel1.Text = this.button1.Location.X.ToString();
            //显示Y坐标
            this.toolStripStatusLabel4.Text = this.button1.Location.Y.ToString();
            //显示步长
            this.toolStripStatusLabel6.Text = step.ToString();
            //显示时间间隔
            this.toolStripStatusLabel8.Text = this.timer1.Interval.ToString();
            //设置P2P运动窗体中的坐标位置
            if (pointState!=PointState.None)  frm_P2PMove?.SetButtonLocation(this.button1.Location.X, this.button1.Location.Y);
            
            
        }

        private void jop运行ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Frm_JopMove frm_JopMove = new Frm_JopMove(enable => this.jop运行ToolStripMenuItem.Enabled= enable);
            frm_JopMove.Show();
            this.jop运行ToolStripMenuItem.Enabled = false;
        }

        private void p2P运动ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frm_P2PMove = new Frm_P2PMove(this.button1.Location.X, this.button1.Location.Y, enable => this.p2P运动ToolStripMenuItem.Enabled = enable);
            frm_P2PMove.Show();
            this.p2P运动ToolStripMenuItem.Enabled = false;
        }

        private void 设置ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Frm_SetSpeed frm_SetSpeed = new Frm_SetSpeed(step, this.timer1.Interval,enable => this.设置ToolStripMenuItem.Enabled= enable);
            frm_SetSpeed.Show();
            this.设置ToolStripMenuItem.Enabled = false;
        }
    }
}
